package com.zzsy.carosshopprojects.view;

import android.app.Activity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zzsy.carosshopprojects.R;

/**
 * 共用的标题栏
 */
public class TitleControlView {
    private View view;
    private FrameLayout left;
    private TextView center;
    private TextView right;
    private RelativeLayout title_body;

    public FrameLayout getLeft() {
        return left;
    }

    private ImageView right_img;
    private Activity activity;

    public TitleControlView(View view, Activity activity) {
        this.view = view;
        this.activity = activity;
        initLayout();
    }

    public TitleControlView(View view, final OnClickLeft onClickLeft) {
        this.view = view;
        left = view.findViewById(R.id.title_left);
        center = view.findViewById(R.id.title_center);
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickLeft.onLeft();

            }
        });
    }

    public RelativeLayout getMainView() {
        title_body = view.findViewById(R.id.title_body);
        return title_body;
    }

    private void initLayout() {
        left = view.findViewById(R.id.title_left);
        center = view.findViewById(R.id.title_center);
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.finish();

            }
        });
    }

    public View getRightView() {
        return right_img;
    }


    public void setTitle(String title) {
        center.setText(title);
    }

    public String getTitle() {
        return center.getText().toString();
    }

    public void setRightText(String s, final OnClickRight clickRight) {
        right = view.findViewById(R.id.title_right);
        right.setVisibility(View.VISIBLE);
        right.setText(s);
        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickRight.onRight();
            }
        });
    }

    public void setRightText(String s) {
        right.setText(s);
    }

    public void setRightImg(int res, final OnClickRight clickRight) {
        right_img = view.findViewById(R.id.title_right_img);
        right_img.setImageResource(res);
        right_img.setVisibility(View.VISIBLE);
        right_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickRight.onRight();
            }
        });
    }

    public interface OnClickRight {
        void onRight();
    }

    public interface OnClickLeft {
        void onLeft();
    }
}
